package com.rjt.common.util;

import java.util.Base64;
import java.util.List;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import blade.kit.http.HttpRequest;
@Component("cuccUtil")
public class CuccUtil {
	
	
	public static String writeData(String mobile,String rate,List<String> paths) {
		String data=mobile+"|"+rate;
		data=Base64.getEncoder().encodeToString(data.getBytes());
		for (String path : paths) {
			FilesUtil.String2Txt(path, data, false);
		}
		return "";
	}
	/**
	 * 查询数量
	 * @param mobile
	 * @return
	 */
	public static int getNum(String mobile,String encryptmobile) {
		String data = getData(mobile,"1",encryptmobile);
		int initNum=Integer.parseInt(JsonUtil.getJsonValue(data, "total"))/5;
		return initNum;
	}
	/**
	 * 查询数据的接口
	 * @param mobile
	 * @param state
	 * @return
	 */
	public static  String getData(String mobile,String state,String encryptmobile) {
		HttpRequest request=HttpRequest.get("https://m.client.10010.com/newuser_century/static/newUserCentury/liststate?usernumberofjsp="+encryptmobile+"&state="+state, true, 
				"mobile", mobile);
		String res = request.body();
		return res;
		
	}
	
	/**
	 * 判断是否完成
	 * @param mobile
	 * @param initNum 
	 * @param num
	 * @return
	 */
	public static boolean isComplete(String mobile,int initNum,String num,String encryptmobile) {
		int currentNum = getNum(mobile,encryptmobile);
		System.out.println("已经充值了"+(currentNum-initNum)+"个");
		if(currentNum-initNum>=Integer.parseInt(num)) {
			return true;
		}
		return false;
	}
	
	public static void sendSimpleMail(String content,String mail,JavaMailSender mailSender)  {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("321947389@qq.com");
        message.setTo(mail);
        message.setSubject("话费充值完毕");
        message.setText(content);
        mailSender.send(message);
    }
	
	public static void sendSimpleMail(String fromMail,String subject,String content,String mail,JavaMailSender mailSender)  {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromMail);
        message.setTo(mail);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }
}
